<template>
  <div class="admin-layout">
    <el-container>
      <el-aside width="200px">
        <div class="logo">
          <h2>购药管理系统</h2>
          <p>管理后台</p>
        </div>
        <el-menu
          router
          :default-active="$route.path"
          class="admin-menu"
        >
          <el-menu-item index="/admin/home">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/admin/categories">
            <el-icon><Menu /></el-icon>
            <span>药品分类管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/drugs">
            <el-icon><FirstAidKit /></el-icon>
            <span>药品管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/orders">
            <el-icon><List /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/announcements">
            <el-icon><Bell /></el-icon>
            <span>公告管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div class="header-content">
            <div class="breadcrumb">
              <el-breadcrumb>
                <el-breadcrumb-item>管理后台</el-breadcrumb-item>
                <el-breadcrumb-item>{{ getPageTitle() }}</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
            <div class="user-info">
              <el-dropdown @command="handleCommand">
                <span class="user-dropdown">
                  {{ username }}
                  <el-icon><CaretBottom /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                    <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../../stores/user'
import { 
  House, 
  Menu, 
  FirstAidKit,
  List, 
  Bell, 
  CaretBottom 
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const username = ref('')

const getPageTitle = () => {
  const pathMap = {
    '/admin/home': '首页',
    '/admin/categories': '药品分类管理',
    '/admin/drugs': '药品管理',
    '/admin/orders': '订单管理',
    '/admin/announcements': '公告管理',
    '/admin/profile': '个人信息'
  }
  return pathMap[route.path] || ''
}

const handleCommand = async (command) => {
  if (command === 'logout') {
    userStore.logout()
    await router.push('/login')
    ElMessage.success('已退出登录')
  } else if (command === 'profile') {
    router.push('/admin/profile')
  }
}

onMounted(() => {
  if (!userStore.isAuthenticated || !userStore.isAdmin) {
    router.push('/login')
    return
  }
  username.value = userStore.user?.username || '管理员'
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.el-container {
  height: 100%;
}

.el-aside {
  background-color: #304156;
  color: #fff;
}

.logo {
  height: 60px;
  padding: 10px;
  text-align: center;
  border-bottom: 1px solid #1f2d3d;
}

.logo h2 {
  margin: 0;
  font-size: 16px;
  color: #fff;
}

.logo p {
  margin: 5px 0 0;
  font-size: 12px;
  color: #97a8be;
}

.admin-menu {
  border-right: none;
  background-color: #304156;
}

.el-menu-item {
  color: #bfcbd9;
}

.el-menu-item:hover {
  color: #fff;
}

.el-menu-item.is-active {
  color: #409EFF;
  background-color: #263445;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  padding: 0 20px;
}

.header-content {
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.breadcrumb {
  font-size: 14px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  cursor: pointer;
  color: #333;
  display: flex;
  align-items: center;
  gap: 4px;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}

:deep(.el-menu-item) {
  display: flex;
  align-items: center;
  gap: 8px;
}

:deep(.el-breadcrumb__item) {
  display: flex;
  align-items: center;
}
</style> 